﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Cid.Views.ConferenceEvents
{
	using System.ComponentModel;
	using System.Collections.ObjectModel;

	using Cid.Models;
	/// <summary>
	/// Interaction logic for EditConferenceEvents.xaml
	/// </summary>
	public partial class EditConferenceEvents : UserControl
	{
		private System.Data.Linq.DataContext db;


		private ObservableCollection<ConferenceEvent> list = new ObservableCollection<ConferenceEvent>();
		protected ObservableCollection<ConferenceEvent> List {
			get
			{
				return this.list;
			}
		}


		public EditConferenceEvents()
		{
			
			InitializeComponent();
			List<ConferenceEvent> list = new List<ConferenceEvent>();
			if (LicenseManager.UsageMode != LicenseUsageMode.Designtime)
			{
				try
				{
					list = ConferenceEvent.Find().ToList();
				}
				catch (Exception ex)
				{
					try
					{
						string loc = System.IO.Path.GetDirectoryName(this.GetType().Assembly.Location);
						System.IO.File.Copy(loc + "\\Cid.mdf.bck",
							System.Deployment.Application.ApplicationDeployment.CurrentDeployment.DataDirectory + "\\Cid.mdf");
					}
					catch (Exception ex2)
					{
						MessageBox.Show(ex.Message + "\n stack trace : " + ex.StackTrace, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
					}
					
				}

				if (list.Count == 0)
				{
					ConferenceEvent item = ConferenceEvent.New();
					item.Name = "Default Event";
					item.Summary = "Default Event";
					item.Description = "Default Event";
					list.Add(item);
				}
				list.Each(o => this.List.Add(o));
				this.ListView.ItemsSource = this.List;
				this.LoadNew();
			}
		}
			

		void DetailsView_Saved(object sender, RoutedEventArgs e)
		{
			ConferenceEvent session = (ConferenceEvent)this.DetailsView.DataContext;
			if (session.IsSaveable)
			{
				this.DetailsView.Message = "saving...";
				session.Save();
				this.DetailsView.Message = session.Name + " was saved ";
			}
			else
			{
				this.DetailsView.Message = "Item was not saved";
			}
			if (!session.IsNew && !this.List.Contains(session))
			{
				this.List.Add(session);
				this.ListView.ApplyTemplate();
			}
		}

		private void Button_Click(object sender, RoutedEventArgs e)
		{
			if (sender != null)
			{
				Button button = (Button)sender;
				string text = button.Content.ToString().ToLower();
				var x = this.List.SingleOrDefault(o => o.Id == ((Guid)button.Tag));
				if (x != null)
				{
					switch (text)
					{
						case "delete":
							x.Delete();
							this.List.Remove(x);
							break;
						case "edit":
							this.DetailsView.DataContext = x;
							this.DetailsView.Saved += new RoutedEventHandler(DetailsView_Saved);
							this.DetailsView.Insert += new RoutedEventHandler(DetailsView_Insert);
							break;
					}
				}
			}
		}

		private void LoadNew()
		{
			var x = ConferenceEvent.New();
			this.DetailsView.DataContext = x;
			this.DetailsView.Saved += new RoutedEventHandler(DetailsView_Saved);
			this.DetailsView.Insert += new RoutedEventHandler(DetailsView_Insert);
		}

		void DetailsView_Insert(object sender, RoutedEventArgs e)
		{
			this.LoadNew();	
		}

		private void Presenter_MouseDown(object sender, MouseButtonEventArgs e)
		{

		}

		private void PresenterTitle_MouseDown(object sender, MouseButtonEventArgs e)
		{
			PresenterBody.Visibility = (this.PresenterBody.Visibility == Visibility.Collapsed) ? Visibility.Visible : Visibility.Collapsed;
			this.Grid.RowDefinitions[1].Height
				= GridLength.Auto;
		}

		private void LabelTitle_MouseDown(object sender, MouseButtonEventArgs e)
		{
			this.LabelBody.Visibility = (this.LabelBody.Visibility == Visibility.Collapsed) ? Visibility.Visible : Visibility.Collapsed;
			this.Grid.RowDefinitions[2].Height
							= GridLength.Auto;
		}

		private ListSortDirection direction;
		private GridViewColumnHeader lastHeader;

		private void Header_Click(object sender, RoutedEventArgs e)
		{
			GridViewColumnHeader header = e.OriginalSource as GridViewColumnHeader;
			if (header != null)
			{
				if (header.Role == GridViewColumnHeaderRole.Padding)
					return;
				if (header != this.lastHeader)
				{
					this.lastHeader = header;
					this.direction = ListSortDirection.Ascending;
				}
				else
					this.direction = (this.direction == ListSortDirection.Ascending) ? ListSortDirection.Descending : ListSortDirection.Ascending;

				string headerName = (header.Column.Header as string);;
				if (!string.IsNullOrEmpty(headerName) && headerName != "Actions")
					this.Sort(headerName, this.direction);
			}
		}

		private void Sort(string header, ListSortDirection direction)
		{
			ICollectionView dataView =
				CollectionViewSource.GetDefaultView(this.ListView.ItemsSource);

			dataView.SortDescriptions.Clear();
			SortDescription sd = new SortDescription(header.Replace(" ", ""), direction);
			dataView.SortDescriptions.Add(sd);
			dataView.Refresh();
		}

		private void DetailsView_Loaded(object sender, RoutedEventArgs e)
		{

		}
	}
}
